<script setup lang="ts">
import { ref } from 'vue';
import { fetchUserGames, fetchUserPlayTime } from '@/service/api';
import { useAuthStore } from '@/store/modules/auth';

defineOptions({
  name: 'GameStatsCard'
});

const authStore = useAuthStore();
const totalPlayTime = ref(0);
const gameCount = ref(0);
const loading = ref(false);

// 格式化游戏时长
const formatPlayTime = (minutes: number) => {
  const hours = Math.floor(minutes / 60);
  const mins = minutes % 60;
  if (hours > 0) {
    return `${hours}小时${mins}分钟`;
  }
  return `${mins}分钟`;
};

// 获取游戏统计数据
const loadGameStats = async () => {
  if (!authStore.userInfo.userId) return;

  loading.value = true;
  try {
    // 获取总游戏时长
    const { data: playTime } = await fetchUserPlayTime(authStore.userInfo.userId);
    if (playTime !== undefined && playTime !== null) {
      totalPlayTime.value = playTime;
    }

    // 获取游戏数量
    const { data: games } = await fetchUserGames(authStore.userInfo.userId);
    if (games) {
      gameCount.value = games.length;
    }
  } catch (error) {
    console.error('获取游戏统计失败:', error);
  } finally {
    loading.value = false;
  }
};

// 预留防抖处理，如后续添加刷新按钮时可用：
// const debouncedLoadGameStats = debounce(loadGameStats, 800, { leading: true, trailing: false });

// 初始化
loadGameStats();
</script>

<template>
  <NCard :bordered="false" class="card-wrapper">
    <template #header>
      <div class="flex items-center space-x-3">
        <div class="header-icon">
          <SvgIcon icon="mdi:gamepad-variant" />
        </div>
        <span class="text-20px text-gray-800 font-bold">游戏统计</span>
      </div>
    </template>

    <NSpin :show="loading">
      <div class="stats-container">
        <!-- 总游戏时长 -->
        <div class="stat-item total-time">
          <div class="stat-background">
            <div class="stat-pattern"></div>
          </div>
          <div class="stat-content">
            <div class="stat-icon">
              <SvgIcon icon="mdi:clock-outline" />
            </div>
            <div class="stat-info">
              <div class="stat-value">{{ formatPlayTime(totalPlayTime) }}</div>
              <div class="stat-label">总游戏时长</div>
            </div>
            <div class="stat-decoration">
              <SvgIcon icon="mdi:clock-outline" />
            </div>
          </div>
        </div>

        <!-- 游戏数量 -->
        <div class="stat-item game-count">
          <div class="stat-background">
            <div class="stat-pattern"></div>
          </div>
          <div class="stat-content">
            <div class="stat-icon">
              <SvgIcon icon="mdi:package-variant" />
            </div>
            <div class="stat-info">
              <div class="stat-value">{{ gameCount }}</div>
              <div class="stat-label">拥有游戏</div>
            </div>
            <div class="stat-decoration">
              <SvgIcon icon="mdi:package-variant" />
            </div>
          </div>
        </div>

        <!-- 平均游戏时长 -->
        <div class="stat-item avg-time">
          <div class="stat-background">
            <div class="stat-pattern"></div>
          </div>
          <div class="stat-content">
            <div class="stat-icon">
              <SvgIcon icon="mdi:chart-line" />
            </div>
            <div class="stat-info">
              <div class="stat-value">
                {{ gameCount > 0 ? formatPlayTime(Math.floor(totalPlayTime / gameCount)) : '0分钟' }}
              </div>
              <div class="stat-label">平均游戏时长</div>
            </div>
            <div class="stat-decoration">
              <SvgIcon icon="mdi:chart-line" />
            </div>
          </div>
        </div>
      </div>
    </NSpin>
  </NCard>
</template>

<style scoped>
.card-wrapper {
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.95), rgba(255, 255, 255, 0.85));
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 24px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  overflow: hidden;
}

.card-wrapper:hover {
  transform: translateY(-4px);
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.12);
}

.card-wrapper::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(90deg, #667eea, #764ba2, #f093fb, #f5576c);
  background-size: 400% 100%;
  animation: gradient-shift 3s ease-in-out infinite;
}

@keyframes gradient-shift {
  0%,
  100% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
}

.header-icon {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  color: white;
  font-size: 20px;
}

.stats-container {
  display: flex;
  flex-direction: column;
  gap: 20px;
  padding: 8px 0;
}

.stat-item {
  position: relative;
  border-radius: 20px;
  overflow: hidden;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.stat-item:hover {
  transform: translateY(-4px) scale(1.02);
}

.stat-background {
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg, var(--gradient-start), var(--gradient-end));
}

.stat-pattern {
  position: absolute;
  top: -50%;
  right: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.1) 1px, transparent 1px);
  background-size: 20px 20px;
  animation: pattern-float 20s linear infinite;
}

@keyframes pattern-float {
  0% {
    transform: translate(0, 0) rotate(0deg);
  }
  100% {
    transform: translate(-20px, -20px) rotate(360deg);
  }
}

.stat-content {
  position: relative;
  display: flex;
  align-items: center;
  padding: 24px;
  color: white;
  z-index: 1;
}

.stat-icon {
  width: 56px;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 16px;
  margin-right: 20px;
  font-size: 28px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  transition: all 0.3s ease;
}

.stat-item:hover .stat-icon {
  transform: scale(1.1) rotate(5deg);
  background: rgba(255, 255, 255, 0.3);
}

.stat-info {
  flex: 1;
}

.stat-value {
  font-size: 32px;
  font-weight: 700;
  margin-bottom: 6px;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  line-height: 1.2;
}

.stat-label {
  font-size: 14px;
  opacity: 0.9;
  font-weight: 500;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
}

.stat-decoration {
  position: absolute;
  top: -20px;
  right: -20px;
  width: 100px;
  height: 100px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  font-size: 40px;
  opacity: 0.3;
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;
}

.stat-item:hover .stat-decoration {
  transform: scale(1.2) rotate(15deg);
  opacity: 0.5;
}

/* 不同统计项的渐变颜色 */
.total-time {
  --gradient-start: #667eea;
  --gradient-end: #764ba2;
}

.game-count {
  --gradient-start: #f093fb;
  --gradient-end: #f5576c;
}

.avg-time {
  --gradient-start: #4facfe;
  --gradient-end: #00f2fe;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .stats-container {
    gap: 16px;
  }

  .stat-content {
    padding: 20px;
  }

  .stat-icon {
    width: 48px;
    height: 48px;
    font-size: 24px;
    margin-right: 16px;
  }

  .stat-value {
    font-size: 28px;
  }
}
</style>
